package com.itheima.mongo.api;

import cn.hutool.core.collection.CollUtil;
import com.itheima.dubbo.api.RecommendUserApi;
import com.itheima.model.mongo.RecommendUser;
import com.itheima.model.mongo.UserLike;
import com.itheima.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //查询今日佳人
    public RecommendUser queryWithMaxScore(Long toUserId) {

        //根据toUserId查询，根据评分score排序，获取第一条
        //构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建Query对象
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        //调用mongoTemplate查询

        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    //查询推荐列表
    @Override
    public PageResult pagerec(Integer page, Integer pagesize, Long toUserId) {
        //1、构建Criteria对象
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //2、创建Query对象
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(pagesize)
                .skip((page - 1) * pagesize);
        //3、调用mongoTemplate查询
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        long count = mongoTemplate.count(query, RecommendUser.class);
        //4、构建返回值PageResult
        return new PageResult(page, pagesize, (int) count, list);
    }

    @Override
    public RecommendUser findbest(Long userId, Long toUserId) {
        //创建查询条件
        //如果佳人列id等于佳人id并且用户列id等于操作用户id
        Criteria criteria = Criteria.where("userId").in(userId).and("toUserId").is(toUserId);
        Query query = Query.query(criteria);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser == null) {
            recommendUser = new RecommendUser();
            recommendUser.setUserId(userId);
            recommendUser.setToUserId(toUserId);
            recommendUser.setScore(99d);
        }
        return recommendUser;
    }

    @Override
    public List<RecommendUser> cards(Long userId, int counts) {
        //创建查询条件,排除喜欢和不喜欢的用户
        Query query = Query.query(Criteria.where("userId").is(userId));
        //查询
        List<UserLike> likes = mongoTemplate.find(query, UserLike.class);
        List<Long> likeUserIds = CollUtil.getFieldValues(likes, "likeUserId", Long.class);
        //构造查询推荐条件
        Criteria criteria = Criteria.where("toUserId").is(userId).and("likeUserId").nin(likeUserIds);
        //查询随机
        TypedAggregation<RecommendUser> aggregation = TypedAggregation.newAggregation(
                RecommendUser.class,
                Aggregation.match(criteria),
                Aggregation.sample(counts)
        );
        AggregationResults<RecommendUser> results = mongoTemplate.aggregate(aggregation, RecommendUser.class);
        return results.getMappedResults();
    }
}